<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\discount\FullCut;
use dosamigos\datetimepicker\DateTimePicker;

/* @var $this yii\web\View */
/* @var $model common\models\discount\FullCut */
/* @var $form yii\widgets\ActiveForm */

$status = FullCut::$STATUS_DESC;
unset($status[-1]);

$model->c_obj= 1;
$model->c_type = 1;
if($model->isNewRecord) {
   $model->c_status = 0;
}


$id = !empty($model->full_cut_id) ? $model->full_cut_id : 0;
?>
<style>
.field-fullcut-end_time .input-group{width:300px;}
.field-fullcut-begin_time .input-group{width:300px;}
.field-fullcut-full_cut_set input[type='text']{width: 200px;height: 34px;padding: 6px 12px;font-size: 14px;line-height: 1.42857143;color: #555;background-color: #fff;background-image: none;border: 1px solid #ccc;}
.set_list{padding-bottom: 4px;}
</style>
<div class="full-cut-form">

    <?php $form = ActiveForm::begin(['id'=>'fullcut-submit']); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'c_obj')->radioList(FullCut::$OBJ_DESC); ?>

    <?= $form->field($model, 'c_type')->radioList(FullCut::$TYPE_DESC); ?>

	<!-- 满减设置 -->
	<div class="form-group field-fullcut-full_cut_set">
		<label class="control-label" for="fullcut-full_cut_set">满减设置</label>
		<?php if(empty($model->fullCutSe)):?>
		<div id="append_list">
    		<div class="set_list">
    			<span class="modular">
        			<span>订单消费金额</span>
        			<input name="total_amount[]" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" type="text" placeholder="订单消费金额"/>
        			<span>优惠金额</span>
        			<input name="discount_amount[]" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"  type="text" placeholder="优惠金额"/>
    			</span>
    			<input name="button" type="button"  id="add_full_set" value="添加" class="btn btn-primary" />
    		</div>
		</div>
		<?php else:?>
		<div id="append_list">
		<?php foreach ($model->fullCutSe as $key=>$val):?>
    		<div class="set_list">
    			<span class="modular">
        			<span>订单消费金额</span>
        			<input value="<?=$val->total_amount;?>" name="total_amount[]" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" type="text" placeholder="订单消费金额"/>
        			<span>优惠金额</span>
        			<input value="<?=$val->discount_amount;?>" name="discount_amount[]" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"  type="text" placeholder="优惠金额"/>
    			</span>
    			<?php if( $key ==0):?>
    			<input name="button" type="button"  id="add_full_set" value="添加" class="btn btn-primary" />
    			<?php else:?>
    			<input name="button" type="button"  value="删除" class="remove_full_set btn btn-danger" />
    			<?php endif;?>
    		</div>
		<?php endforeach;?>
		 </div>
		<?php endif;?>
		<div class="help-block"></div>
	</div>

	<?= $form->field($model, 'begin_time')->widget(DateTimePicker::className(), 
        [
        'template'=>"{input}{reset}{button}",
        'clientOptions' => [
            'autoclose' => true,
            'linkFormat' => 'yyyy-MM-dd HH:ii', // if inline = true
            // 'format' => 'HH:ii P', // if inline = false
            'todayBtn' => true,
            'minView' =>'hour',
            'pickerPosition'=>'bottom-left',
            'language'=>'zh-CN'
        ]    
    ])
    
    ?>
       <?= $form->field($model, 'end_time')->widget(DateTimePicker::className(), 
        [
        'template'=>"{input}{reset}{button}",
        'clientOptions' => [
            'autoclose' => true,
            'linkFormat' => 'yyyy-MM-dd HH:ii', // if inline = true
            // 'format' => 'HH:ii P', // if inline = false
            'todayBtn' => true,
            'minView' =>'hour',
            'pickerPosition'=>'bottom-left',
            'language'=>'zh-CN'
        ]    
    ])
    ?>

    <?= $form->field($model, 'c_status')->radioList($status) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? '添加' : '修改', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
	</div>

    <?php ActiveForm::end(); ?>

</div>
<?php $this->beginBlock('js');?>
<script>
$(function(){
	$("#fullcut-submit").submit(function(e){
		//判断是否有空
		for(var i in $('.field-fullcut-full_cut_set input[type="text"]')) {
	        if(!isNaN(i)) {
	            if($($('.field-fullcut-full_cut_set input[type="text"]')[i]).val() == '') {
	                alert('满减设置都是必填!多余选项请删除!');
	                $($('.field-fullcut-full_cut_set input[type="text"]')[i]).focus();
	                return false;
	            }
	        }
	    }
	    //判断开始不能大于结束时间
		var begin = $("#fullcut-begin_time").val();
		var end = $("#fullcut-end_time").val();
		var flag = true;
		var csrfToken = $('meta[name="csrf-token"]').attr("content");
		$.ajax({
            url: '<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/check-date')?>',
            type: 'post',
            data: {_csrf:csrfToken,begin_time:begin,end_time:end,id:<?=$id?>},
            dataType: 'json',
            async:false,
            success: function (e) {
                if(e.flag) {
                    flag = true;
                } else {
                    flag = false;
                    $(".field-fullcut-end_time .help-block").html(e.msg).css({'color':'red','font-size':'12px'});
                }
            }
        });
        return flag;

	});	

	$(".field-fullcut-c_status .help-block").html('*需设置活动状态为上线预设时间才生效').css({'color':'red','font-size':'12px'});
	//添加
	$("#add_full_set").click(function(){
		if($("#append_list .set_list").size() > 2) {
			alert('最多添加3重优惠');
			return false;
		}
		var html = $(this).prev('span').html();
		var str = '<div class="set_list"><span class="modular">'+html;
		    str +='</span><input name="button" type="button"  class="remove_full_set btn btn-danger" value="删除" /></div>'
		 $(".field-fullcut-full_cut_set #append_list").append(str);
		 $(".field-fullcut-full_cut_set #append_list .set_list:last-child").find("input[type='text']").val('');
			return false;
	});
	
	//删除
	$("body").delegate(".remove_full_set",'click',function(){
		 $(this).parent().remove();
	});
});

function isEmpty(info){
	if(info==null||info==undefined||(""+info)==""){
		return true;
	}
	return false;
}

</script>
<?php $this->endBlock(); ?>
